/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    class EqualPay : BindInterface
    {
        public EqualPay(string name = "")
        {
            Text = name;
        }

        override public System.Windows.Visibility IsVisible
        {
            get
            {
                if (CloseCheck.StateObject.SelectedOrderNotNull &&
                    CloseCheck.GlobalStateObject.SystemHasLocalPrinter &&
                    CloseCheck.StateObject.SelectedOrderHasNoTransactions &&
                    CloseCheck.StateObject.SelectedOrderCheckCountIsOne)
                {
                    if (SHIFT.SelectedOrder.SplitBySeatHasBeenApplied != true &&
                        SHIFT.SelectedOrder.EqualPayIsActive != true)
                    {
                        Text = "Equal Receipts";
                        OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Text"));
                        return System.Windows.Visibility.Visible;
                    }
                    else if (SHIFT.SelectedOrder.SplitBySeatHasBeenApplied != true &&
                        SHIFT.SelectedOrder.EqualPayIsActive == true)
                    {
                        Text = "Remove Equal";
                        OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Text"));
                        return System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        return System.Windows.Visibility.Hidden;
                    }
                }
                else
                {
                    return System.Windows.Visibility.Hidden;
                }
            }
        }

        override public void Clicked()
        {
            try
            {
                if (SHIFT.SelectedOrder.EqualPayIsActive == true)
                {
                    ///Remove equal Pay
                    SHIFT.SelectedOrder.EqualPayIsActive = false;
                    SHIFT.SelectedOrder.EqualPayCount = null;
                }
                else
                {
                    HowManyBox hmb = new HowManyBox();
                    hmb.ShowDialog();
                    if (HowManyBox.LastNumberSelection > 1 && HowManyBox.LastNumberSelection < 99)
                    {
                        SHIFT.SelectedOrder.EqualPayCount = HowManyBox.LastNumberSelection;
                        SHIFT.SelectedOrder.EqualPayIsActive = true;

                        ///Split the amounts
                        decimal partialRoundedAutoGratuity = SHIFT.SelectedOrder.SelCheckAutoGratutiy / HowManyBox.LastNumberSelection;
                        decimal partialRoundedSubtotal = SHIFT.SelectedOrder.SelCheckRoundedSubtotal / HowManyBox.LastNumberSelection;
                        decimal partialRoundedTax = SHIFT.SelectedOrder.SelCheckTax / HowManyBox.LastNumberSelection;

                        ///Round updward
                        partialRoundedAutoGratuity = Math.Round(partialRoundedAutoGratuity, 2, MidpointRounding.AwayFromZero);
                        partialRoundedSubtotal = Math.Round(partialRoundedSubtotal, 2, MidpointRounding.AwayFromZero);
                        partialRoundedTax = Math.Round(partialRoundedTax, 2, MidpointRounding.AwayFromZero);

                        ///Calculate the error and adjust it on the last check
                        decimal autoGratuityError =
                            SHIFT.SelectedOrder.SelCheckAutoGratutiy - (partialRoundedAutoGratuity * HowManyBox.LastNumberSelection);

                        decimal subtotalError =
                            SHIFT.SelectedOrder.SelCheckRoundedSubtotal - (partialRoundedSubtotal * HowManyBox.LastNumberSelection);

                        decimal taxError =
                            SHIFT.SelectedOrder.SelCheckTax - (partialRoundedTax * HowManyBox.LastNumberSelection);

                        for (int i = 0; i < HowManyBox.LastNumberSelection; i++)
                        {
                            ///Now that equal pay is active, we need to print equal itemized receipts.
                            if (i == (HowManyBox.LastNumberSelection - 1))
                            {
                                partialRoundedAutoGratuity += autoGratuityError;
                                partialRoundedSubtotal += subtotalError;
                                partialRoundedTax += taxError;
                            }

                            KIOSK k = KIOSK.Get(SHIFT.GlobalContext);
                            PrintSpool.PrintItemizedReceipt((long)k.LocalPrinterId,
                                                            SHIFT.SelectedOrder,
                                                            partialRoundedAutoGratuity,
                                                            partialRoundedSubtotal,
                                                            partialRoundedTax,
                                                            CCBindings.SelectedSeatIndex,
                                                            HowManyBox.LastNumberSelection);
                        }
                    }
                    else
                    {
                        UMD.ShowGuiMessageSTASafe_NOTHROW("You must select a count greater than 1 and less than 100.");
                    }
                }

                CCBindings.RefreshThreadSafe();
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }
    }
}
